<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, user-scalable=no">
  <meta name="description" content="API docs for the getLyrics method from the ReaderLyricsSource class, for the Dart programming language.">
  <title>getLyrics method - ReaderLyricsSource class - media library - Dart API</title>


  
  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,300;0,400;0,500;0,700;1,400&display=swap" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0" rel="stylesheet">
  
  <link rel="stylesheet" href="../../static-assets/github.css?v1">
  <link rel="stylesheet" href="../../static-assets/styles.css?v1">
  <link rel="icon" href="../../static-assets/favicon.png?v1">

  
</head>


<body data-base-href="../../" data-using-base-href="false" class="light-theme">

<div id="overlay-under-drawer"></div>

<header id="title">
  <span id="sidenav-left-toggle" class="material-symbols-outlined" role="button" tabindex="0">menu</span>
  <ol class="breadcrumbs gt-separated dark hidden-xs">
    <li><a href="../../index.html">yuuna</a></li>
    <li><a href="../../media/media-library.html">media</a></li>
    <li><a href="../../media/ReaderLyricsSource-class.html">ReaderLyricsSource</a></li>
    <li class="self-crumb">getLyrics method</li>
  </ol>
  <div class="self-name">getLyrics</div>
  <form class="search navbar-right" role="search">
    <input type="text" id="search-box" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
  <div class="toggle" id="theme-button">
    <label for="theme">
      <input type="checkbox" id="theme" value="light-theme">
      <span id="dark-theme-button" class="material-symbols-outlined">
        brightness_4
      </span>
      <span id="light-theme-button" class="material-symbols-outlined">
        brightness_5
      </span>
    </label>
  </div>
</header>
<main>

  <div id="dartdoc-main-content" class="main-content">
      <div>
<h1><span class="kind-method">getLyrics</span> method 
</h1></div>

    <section class="multi-line-signature">
      

<span class="returntype"><a href="https://api.flutter.dev/flutter/dart-async/Future-class.html">Future</a><span class="signature">&lt;<wbr><span class="type-parameter"><a href="../../utils/JidoujishoLyrics-class.html">JidoujishoLyrics</a></span>&gt;</span></span>
<span class="name ">getLyrics</span>(<wbr><ol class="parameter-list"><li><span class="parameter" id="getLyrics-param-parameters"><span class="type-annotation"><a href="../../utils/JidoujishoLyricsParameters-class.html">JidoujishoLyricsParameters</a></span> <span class="parameter-name">parameters</span></span></li>
</ol>)

      

    </section>
    
<section class="desc markdown">
  <p>Get lyrics</p>
</section>


    
<section class="summary source-code" id="source">
  <h2><span>Implementation</span></h2>
  <pre class="language-dart"><code class="language-dart">Future&lt;JidoujishoLyrics&gt; getLyrics(
  JidoujishoLyricsParameters parameters,
) async {
  String userAgent =
      &#39;Mozilla&#47;5.0 (Windows NT 10.0; Win64; x64) AppleWebKit&#47;537.36 (KHTML, like Gecko) Chrome&#47;112.0.0.0 Safari&#47;537.36&#39;;
  String artist = Uri.encodeComponent(parameters.artist.trim());
  String title = Uri.encodeComponent(parameters.title.trim());

  late String searchUrl;
  if (artist.isEmpty) {
    searchUrl = &#39;https:&#47;&#47;www.google.com&#47;search?q=$title+lyrics&#39;;
  } else {
    searchUrl = &#39;https:&#47;&#47;www.google.com&#47;search?q=$title+-+$artist+lyrics&#39;;
  }

  String? text;
  bool webViewBusy = true;

  HeadlessInAppWebView webView = HeadlessInAppWebView(
    initialOptions: InAppWebViewGroupOptions(
      crossPlatform: InAppWebViewOptions(
        userAgent: userAgent,
      ),
      android: AndroidInAppWebViewOptions(
        blockNetworkImage: true,
      ),
    ),
    initialUrlRequest: URLRequest(
      url: Uri.parse(searchUrl),
    ),
    onLoadStop: (controller, uri) async {
      text = await controller.evaluateJavascript(source: &#39;&#39;&#39;
[...document.getElementsByClassName(&#39;ujudUb&#39;)].map((e) =&gt; {
  return e.innerText;
}).join(&#39;\\n\\n&#39;);
&#39;&#39;&#39;);

      if (text != null &amp;&amp; text!.trim().isEmpty) {
        text = null;
      }

      webViewBusy = false;
    },
  );

  await webView.run();

  while (webViewBusy) {
    await Future.delayed(const Duration(milliseconds: 100), () {});
  }

  if (parameters.artist.endsWith(&#39; - Topic&#39;) &amp;&amp; text == null) {
    return getLyrics(
      JidoujishoLyricsParameters(
        artist: parameters.artist.replaceAll(&#39; - Topic&#39;, &#39;&#39;),
        title: parameters.title,
      ),
    );
  }

  if (artist.isNotEmpty &amp;&amp; text == null) {
    String? firstResultUrl;
    bool googleWebViewBusy = true;

    HeadlessInAppWebView googleWebView = HeadlessInAppWebView(
      initialOptions: InAppWebViewGroupOptions(
        crossPlatform: InAppWebViewOptions(
          userAgent: userAgent,
        ),
        android: AndroidInAppWebViewOptions(
          blockNetworkImage: true,
        ),
      ),
      initialUrlRequest: URLRequest(
        url: Uri.parse(
            &#39;https:&#47;&#47;google.com&#47;search?q=$artist+$title+歌詞+site:uta-net.com&#47;song&#39;),
      ),
      onLoadStop: (controller, uri) async {
        firstResultUrl = await controller.evaluateJavascript(
            source:
                &#39;document.querySelector(&quot;.yuRUbf:nth-of-type(1) &gt; a&quot;).href&#39;);

        googleWebViewBusy = false;
      },
    );

    await googleWebView.run();

    while (googleWebViewBusy) {
      await Future.delayed(const Duration(milliseconds: 100), () {});
    }

    if (firstResultUrl != null) {
      bool utanetWebViewBusy = true;
      HeadlessInAppWebView utanetWebView = HeadlessInAppWebView(
        initialOptions: InAppWebViewGroupOptions(
          crossPlatform: InAppWebViewOptions(
            userAgent: userAgent,
          ),
          android: AndroidInAppWebViewOptions(
            blockNetworkImage: true,
          ),
        ),
        initialUrlRequest: URLRequest(
          url: Uri.parse(firstResultUrl!),
        ),
        onLoadStop: (controller, uri) async {
          text = await controller.evaluateJavascript(source: &#39;&#39;&#39;
[...document.getElementsByTagName(&#39;h2&#39;)].map((e) =&gt; e.remove());
var kashi = document.getElementsByClassName(&#39;row kashi&#39;)[0];
kashi.children[0].children[0].innerText;
&#39;&#39;&#39;);

          if (text != null &amp;&amp; text!.trim().isEmpty) {
            text = null;
          }

          utanetWebViewBusy = false;
        },
      );

      await utanetWebView.run();

      while (utanetWebViewBusy) {
        await Future.delayed(const Duration(milliseconds: 100), () {});
      }
    }
  }

  &#47;&#47;&#47; Try again, but without an artist. This will probably be wrong. But it&#39;s
  &#47;&#47;&#47; better to have tried and gotten a good result sometimes than for this
  &#47;&#47;&#47; to always fail just because the artist&#39;s name might make it difficult.
  if (artist.isNotEmpty &amp;&amp; text == null) {
    return getLyrics(
      JidoujishoLyricsParameters(
        artist: &#39;&#39;,
        title: parameters.title,
      ),
    );
  }

  return JidoujishoLyrics(
    includesArtist: artist.isNotEmpty,
    text: text,
  );
}</code></pre>
</section>


  </div> <!-- /.main-content -->

  <div id="dartdoc-sidebar-left" class="sidebar sidebar-offcanvas-left">
    <header id="header-search-sidebar" class="hidden-l">
  <form class="search-sidebar" role="search">
    <input type="text" id="search-sidebar" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
</header>

<ol class="breadcrumbs gt-separated dark hidden-l" id="sidebar-nav">
  <li><a href="../../index.html">yuuna</a></li>
  <li><a href="../../media/media-library.html">media</a></li>
  <li><a href="../../media/ReaderLyricsSource-class.html">ReaderLyricsSource</a></li>
  <li class="self-crumb">getLyrics method</li>
</ol>


    <h5>ReaderLyricsSource class</h5>
    <ol>



        <li class="section-title">
          <a href="../../media/ReaderLyricsSource-class.html#instance-properties">Properties</a>
        </li>
          <li class="inherited"><a href="../../media/ReaderMediaSource/aspectRatio.html">aspectRatio</a></li>
          <li class="inherited"><a href="../../media/MediaSource/currentExtraData.html">currentExtraData</a></li>
          <li class="inherited"><a href="../../media/MediaSource/currentMediaItem.html">currentMediaItem</a></li>
          <li class="inherited"><a href="../../media/MediaSource/currentSentence.html">currentSentence</a></li>
          <li class="inherited"><a href="../../media/MediaSource/description.html">description</a></li>
          <li class="inherited"><a href="../../media/MediaSource/descriptionLocalisation.html">descriptionLocalisation</a></li>
          <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/hashCode.html">hashCode</a></li>
          <li class="inherited"><a href="../../media/MediaSource/icon.html">icon</a></li>
          <li class="inherited"><a href="../../media/MediaSource/implementsHistory.html">implementsHistory</a></li>
          <li class="inherited"><a href="../../media/MediaSource/implementsSearch.html">implementsSearch</a></li>
          <li><a href="../../media/ReaderLyricsSource/isOverride.html">isOverride</a></li>
          <li class="inherited"><a href="../../media/MediaSource/mediaType.html">mediaType</a></li>
          <li><a href="../../media/ReaderLyricsSource/overrideArtist.html">overrideArtist</a></li>
          <li class="inherited"><a href="../../media/MediaSource/overridesAutoAudio.html">overridesAutoAudio</a></li>
          <li class="inherited"><a href="../../media/MediaSource/overridesAutoImage.html">overridesAutoImage</a></li>
          <li><a href="../../media/ReaderLyricsSource/overrideStream.html">overrideStream</a></li>
          <li><a href="../../media/ReaderLyricsSource/overrideTitle.html">overrideTitle</a></li>
          <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/runtimeType.html">runtimeType</a></li>
          <li class="inherited"><a href="../../media/MediaSource/shouldGenerateAudio.html">shouldGenerateAudio</a></li>
          <li class="inherited"><a href="../../media/MediaSource/shouldGenerateImage.html">shouldGenerateImage</a></li>
          <li class="inherited"><a href="../../media/MediaSource/sourceName.html">sourceName</a></li>
          <li class="inherited"><a href="../../media/MediaSource/sourceNameLocalisatiton.html">sourceNameLocalisatiton</a></li>
          <li class="inherited"><a href="../../media/MediaSource/uniqueKey.html">uniqueKey</a></li>

        <li class="section-title"><a href="../../media/ReaderLyricsSource-class.html#instance-methods">Methods</a></li>
          <li class="inherited"><a href="../../media/MediaSource/buildBar.html">buildBar</a></li>
          <li><a href="../../media/ReaderLyricsSource/buildHistoryPage.html">buildHistoryPage</a></li>
          <li><a href="../../media/ReaderLyricsSource/buildLaunchPage.html">buildLaunchPage</a></li>
          <li><a href="../../media/ReaderLyricsSource/buildSearchButton.html">buildSearchButton</a></li>
          <li class="inherited"><a href="../../media/MediaSource/clearCurrentSentence.html">clearCurrentSentence</a></li>
          <li class="inherited"><a href="../../media/MediaSource/clearExtraData.html">clearExtraData</a></li>
          <li><a href="../../media/ReaderLyricsSource/clearOverrideTitleAndAuthor.html">clearOverrideTitleAndAuthor</a></li>
          <li class="inherited"><a href="../../media/MediaSource/clearOverrideValues.html">clearOverrideValues</a></li>
          <li class="inherited"><a href="../../media/MediaSource/deletePreference.html">deletePreference</a></li>
          <li class="inherited"><a href="../../media/MediaSource/generateAudio.html">generateAudio</a></li>
          <li class="inherited"><a href="../../media/MediaSource/generateImages.html">generateImages</a></li>
          <li class="inherited"><a href="../../media/MediaSource/generateSearchSuggestions.html">generateSearchSuggestions</a></li>
          <li><a href="../../media/ReaderLyricsSource/getActions.html">getActions</a></li>
          <li class="inherited"><a href="../../media/MediaSource/getDisplaySubtitleFromMediaItem.html">getDisplaySubtitleFromMediaItem</a></li>
          <li class="inherited"><a href="../../media/MediaSource/getDisplayThumbnailFromMediaItem.html">getDisplayThumbnailFromMediaItem</a></li>
          <li class="inherited"><a href="../../media/MediaSource/getDisplayTitleFromMediaItem.html">getDisplayTitleFromMediaItem</a></li>
          <li class="inherited"><a href="../../media/MediaSource/getLocalisedDescription.html">getLocalisedDescription</a></li>
          <li class="inherited"><a href="../../media/MediaSource/getLocalisedSourceName.html">getLocalisedSourceName</a></li>
          <li><a href="../../media/ReaderLyricsSource/getLyrics.html">getLyrics</a></li>
          <li class="inherited"><a href="../../media/MediaSource/getOverrideThumbnailFilename.html">getOverrideThumbnailFilename</a></li>
          <li class="inherited"><a href="../../media/MediaSource/getOverrideThumbnailFromMediaItem.html">getOverrideThumbnailFromMediaItem</a></li>
          <li class="inherited"><a href="../../media/MediaSource/getOverrideTitleFromMediaItem.html">getOverrideTitleFromMediaItem</a></li>
          <li class="inherited"><a href="../../media/MediaSource/getOverrideTitleKey.html">getOverrideTitleKey</a></li>
          <li class="inherited"><a href="../../media/MediaSource/getPreference.html">getPreference</a></li>
          <li class="inherited"><a href="../../media/MediaSource/getThumbnailUri.html">getThumbnailUri</a></li>
          <li class="inherited"><a href="../../media/MediaSource/initialise.html">initialise</a></li>
          <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/noSuchMethod.html">noSuchMethod</a></li>
          <li class="inherited"><a href="../../media/MediaSource/onMediaItemClear.html">onMediaItemClear</a></li>
          <li><a href="../../media/ReaderLyricsSource/onSearchBarTap.html">onSearchBarTap</a></li>
          <li><a href="../../media/ReaderLyricsSource/onSourceExit.html">onSourceExit</a></li>
          <li><a href="../../media/ReaderLyricsSource/prepareResources.html">prepareResources</a></li>
          <li><a href="../../media/ReaderLyricsSource/requestPermissions.html">requestPermissions</a></li>
          <li class="inherited"><a href="../../media/MediaSource/searchMediaItems.html">searchMediaItems</a></li>
          <li class="inherited"><a href="../../media/MediaSource/setCurrentSentence.html">setCurrentSentence</a></li>
          <li class="inherited"><a href="../../media/MediaSource/setExtraData.html">setExtraData</a></li>
          <li class="inherited"><a href="../../media/MediaSource/setOverrideThumbnailFromMediaItem.html">setOverrideThumbnailFromMediaItem</a></li>
          <li><a href="../../media/ReaderLyricsSource/setOverrideTitleAndArtist.html">setOverrideTitleAndArtist</a></li>
          <li class="inherited"><a href="../../media/MediaSource/setOverrideTitleFromMediaItem.html">setOverrideTitleFromMediaItem</a></li>
          <li class="inherited"><a href="../../media/MediaSource/setPreference.html">setPreference</a></li>
          <li class="inherited"><a href="../../media/MediaSource/setShouldGenerateAudio.html">setShouldGenerateAudio</a></li>
          <li class="inherited"><a href="../../media/MediaSource/setShouldGenerateImage.html">setShouldGenerateImage</a></li>
          <li><a href="../../media/ReaderLyricsSource/showSearchDialog.html">showSearchDialog</a></li>
          <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/toString.html">toString</a></li>

        <li class="section-title inherited"><a href="../../media/ReaderLyricsSource-class.html#operators">Operators</a></li>
          <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/operator_equals.html">operator ==</a></li>


        <li class="section-title"><a href="../../media/ReaderLyricsSource-class.html#static-properties">Static properties</a></li>
          <li><a href="../../media/ReaderLyricsSource/instance.html">instance</a></li>


</ol>

  </div><!--/.sidebar-offcanvas-->

  <div id="dartdoc-sidebar-right" class="sidebar sidebar-offcanvas-right">
  </div><!--/.sidebar-offcanvas-->

</main>

<footer>
  <span class="no-break">
    yuuna
      2.8.0+93
  </span>

  
</footer>



<script src="../../static-assets/highlight.pack.js?v1"></script>
<script src="../../static-assets/docs.dart.js"></script>



</body>

</html>

